var RunLayer = cc.Layer.extend({
	arr:null,
	scene:null,
	clock:null,
	running:null,
	ctor:function (parent) {
		this._super();
		this.scene = parent;
		this.scene.addChild(this, 10);
		gg.main = this;
		this.init();
	},
	init:function () {
		this.callNext = cc.callFunc(function(){
			gg.flow.next();
		}, this);
		this.callNext.retain();
		this.clock = new Clock(this);
		// 物品库
		this.lib = new Lib(this);
		this.outlet=new cc.Sprite("#equitment/outlet.png");
		this.outlet.setPosition(cc.p(gg.width*0.42, gg.height*0.6));
		this.addChild(this.outlet, 10, TAG_OUTLET);
		this.instrument=new Instrument(this,"#equitment/lite.png",TAG_MP_INSTRUMENT);
		this.initAction();
		
	},
	initAction:function(){
		var animFrames=[];
		for(var i=1;i<5;i++){
			var str="holder/holder"+i+".png";
			var frame=cc.spriteFrameCache.getSpriteFrame(str);
			animFrames.push(frame);
		}
		var animation=new cc.Animation(animFrames,0.1)
		this.runningAction=cc.repeat(cc.animate(animation),1);
		this.runningAction.retain();
	},
	//电导仪
	loadInstrument:function(pos){
		var instrument=new Button(this,11,TAG_INSTRUMENT,"#equitment/Instrument.png",this.callback);
		this.loadInLib(instrument,pos,cc.p(gg.c_width, gg.c_height*0.6));
	},
	//电源线
	loadPowerLine:function(pos){
		var powerLine=new Button(this,10,TAG_POWERLINE,"#equitment/powerLine.png",this.callback);
		this.loadInLib(powerLine, pos,cc.p(gg.c_width*0.83,gg.c_height*0.95));
	},
	//电极底座
	loadSeat:function(pos){
		var seat=new Button(this,10,TAG_SEAT,"#equitment/Seat.png",this.callback);
		this.loadInLib(seat, pos,cc.p(gg.c_width*1.35,gg.c_height*0.6));
	},
	//电极架
	loadHolder:function(pos){
		var holder=new Holder(this,10,TAG_HOLDER,this.callback);
		this.loadInLib(holder, pos,cc.p(gg.c_width*1.55,gg.c_height*0.6));
	},
	//电导电极
	loadElectrode:function(pos){
		var electrode=new Electrode(this,10,TAG_ELECTRODE,this.callback);
		
		this.loadInLib(electrode, pos,cc.p(gg.c_width*1.55,gg.c_height*0.7));
	},
	//烧杯
	loadBeaker:function(pos){
		if(gg.flow.flow.action==ACTION_DO1){
			var beaker=new Beaker(this, 10, TAG_BEAKER, this.callback);
		}else if(gg.flow.flow.action==ACTION_DO2){
			var beaker=new Beaker(this, 10, TAG_BEAKER1, this.callback);
		}else if(gg.flow.flow.action==ACTION_DO8){
			var beaker=new Beaker(this, 10, TAG_BEAKER3, this.callback);
		}else if(gg.flow.flow.action==ACTION_DO3){
			var beaker=new Beaker(this, 10, TAG_BEAKER2, this.callback);
		}else if(gg.flow.flow.action==ACTION_DO9){
			var beaker=new Beaker(this, 10, TAG_BEAKER4, this.callback);
		}else if(gg.flow.flow.action==ACTION_DO10){
			var beaker=new Beaker(this, 10, TAG_BEAKER5, this.callback);
		}else if(gg.flow.flow.action==ACTION_DO11){
			var beaker=new Beaker(this, 10, TAG_BEAKER6, this.callback);
		}
		else if(gg.flow.flow.action==ACTION_DO12){
			
			var beaker=new Beaker(this, 10, TAG_BEAKER7, this.callback);
		}
		this.loadInLib(beaker, pos,cc.p(gg.c_width*1.5,gg.c_height*0.6));
	},
	//试剂瓶
	loadKettle:function(pos){
		if(gg.flow.flow.action==ACTION_DO1){
			var kettle=new Button(this,10,TAG_KETTLE,"#equitment/kettle.png",this.callback);
		}else if(gg.flow.flow.action==ACTION_DO8){
			var kettle=new Button(this,10,TAG_KETTLE1,"#equitment/kettle.png",this.callback);
		}else if(gg.flow.flow.action==ACTION_DO10){
			var kettle=new Button(this,10,TAG_KETTLE2,"#equitment/kettle.png",this.callback);
		}else if(gg.flow.flow.action==ACTION_DO12){
			var kettle=new Button(this,10,TAG_KETTLE3,"#equitment/kettle.png",this.callback);
		}
			this.loadInLib(kettle, pos,cc.p(gg.c_width*1.7,gg.c_height*0.7));
	},
	//校准液1
	loadCalibration:function(pos){
		var calibration=new Calibration(this);
		this.addChild(calibration,10);
		this.loadInLib(calibration, pos,cc.p(gg.c_width*1.8,gg.c_height*0.7));
	},
	//校准液2
	loadCalibration1:function(pos){
		var calibration=new Calibration1(this);
		this.addChild(calibration,10);
		this.loadInLib(calibration, pos,cc.p(gg.c_width*1.8,gg.c_height*0.7));
	},
	//校准液3
	loadCalibration2:function(pos){
		var calibration=new Calibration2(this);
		this.addChild(calibration,10);
		this.loadInLib(calibration, pos,cc.p(gg.c_width*1.8,gg.c_height*0.7));
	},
	loadBottle:function(pos){
		var bottle=new Bottle(this);
		this.addChild(bottle,10);
		this.loadInLib(bottle, pos,cc.p(gg.c_width*1.8, gg.c_height*0.6));
		
	},
	loadInLib:function(obj, pos, tarPos,delay){
		obj.setPosition(pos);
		if(delay == null){
			delay = 1;
		}
		var ber = $.bezier(pos, tarPos, delay);
		var seq = cc.sequence(ber, this.callNext);
		obj.runAction(seq);
	},
	callback:function (p){
		var func = cc.callFunc(this.actionDone, this);
		var func1=cc.callFunc(this.flowNext, this);
		switch(p.getTag()){
		case TAG_POWERLINE:
			if(gg.flow.flow.action == ACTION_DO1){
				this.instrument.show(true);
			}else if(gg.flow.flow.action == ACTION_DO2){
				gg.flow.next();
				p.setSpriteFrame("equitment/powerLine1.png");
				p.setPosition(cc.p(gg.c_width*0.89,gg.c_height*0.98));
			}else if(gg.flow.flow.action==ACTION_DO3){
				gg.flow.next();
				p.setSpriteFrame("equitment/powerLine.png");
				p.setPosition(cc.p(gg.c_width*0.83,gg.c_height*0.95));
			}
			break;
			
		case TAG_HOLDER:
			var holder=this.getChildByTag(TAG_HOLDER);
			if(gg.flow.flow.action==ACTION_DO1){
				holder.insert();
			}else if(gg.flow.flow.action==ACTION_DO2){
				p.runAction(cc.sequence(this.runningAction,this.callNext));
			}
			break;
		case TAG_ELECTRODE:
			var electrode=this.getChildByTag(TAG_ELECTRODE);
		//	var beaker=this.getChildByTag(TAG_KETTLE);
			if(gg.flow.flow.action == ACTION_DO1){
				gg.flow.next();
				p.setSpriteFrame("equitment/electrode_3.png");
				var instrument=this.getChildByTag(TAG_MP_INSTRUMENT);
				var main=instrument.getChildByTag(TAG_MAIN);
				var electrode_line=main.getChildByTag(TAG_ELECTRODE_HEAD);
				var electrode_line1=main.getChildByTag(TAG_ELECTRODE_HEAD1);
				electrode_line.setVisible(true);
				electrode_line1.setVisible(true);
			
			}else if(gg.flow.flow.action == ACTION_DO2){
				electrode.insert();
			}else if(gg.flow.flow.action==ACTION_DO3){
				var stirr = new cc.Sprite("#equitment/stir.png")
				stirr.setPosition(cc.p(gg.c_width*1.24,gg.c_height*0.470));
				stirr.setOpacity(0);
				this.addChild(stirr, 5, TAG_STIRR);
				var seq = cc.sequence(cc.delayTime(1.8),cc.spawn(cc.fadeTo(1.5,255),cc.scaleTo(3,1.5,2.3)),cc.spawn(cc.fadeTo(2.0,0),cc.scaleTo(3,0,0)),cc.callFunc(function() {stirr.removeFromParent(true);}));
				stirr.runAction(seq);
				electrode.stir();
			}else if(gg.flow.flow.action==ACTION_DO5){
				var beaker=this.getChildByTag(TAG_BEAKER);
				beaker.setVisible(false);
				
				var scrub=new cc.Sprite("#equitment/scrub.png");
				scrub.setOpacity(200);
				scrub.setPosition(cc.p(gg.c_width*1.45,gg.c_height*0.8));
				this.addChild(scrub,5,TAG_SCRUB);
				var moveto=cc.moveTo(0.8,cc.p(gg.c_width*1.45,gg.c_height*1.1));
				var moveto1=cc.moveTo(0.8,cc.p(gg.c_width*1.45,gg.c_height*0.8));
				scrub.runAction(cc.sequence(moveto,moveto1,moveto,moveto1, cc.callFunc(function() {scrub.removeFromParent(true);})));
					
					
					
			
                
				electrode.whipping();
			}else if(gg.flow.flow.action==ACTION_DO4){
				var stirr = new cc.Sprite("#equitment/stir.png")
				stirr.setPosition(cc.p(gg.c_width*1.24,gg.c_height*0.470));
				stirr.setOpacity(0);
				this.addChild(stirr, 5, TAG_STIRR);
				var seq = cc.sequence(cc.delayTime(1.8),cc.spawn(cc.fadeTo(1.5,255),cc.scaleTo(3,1.5,2.3)),cc.spawn(cc.fadeTo(2.0,0),cc.scaleTo(3,0,0)),cc.callFunc(function() {stirr.removeFromParent(true);}));
				stirr.runAction(seq);
				electrode.stir2();
			}else if(gg.flow.flow.action==ACTION_DO6){
				var stirr = new cc.Sprite("#equitment/stir.png")
				stirr.setPosition(cc.p(gg.c_width*1.24,gg.c_height*0.470));
				stirr.setOpacity(0);
				this.addChild(stirr, 5, TAG_STIRR);
				var seq = cc.sequence(cc.delayTime(1.8),cc.spawn(cc.fadeTo(1.5,255),cc.scaleTo(3,1.5,2.3)),cc.spawn(cc.fadeTo(2.0,0),cc.scaleTo(3,0,0)),cc.callFunc(function() {stirr.removeFromParent(true);}));
				stirr.runAction(seq);
				electrode.stir3();
			}else if(gg.flow.flow.action==ACTION_DO7){
				var stirr = new cc.Sprite("#equitment/stir.png")
				stirr.setPosition(cc.p(gg.c_width*1.24,gg.c_height*0.470));
				stirr.setOpacity(0);
				this.addChild(stirr, 5, TAG_STIRR);
				var seq = cc.sequence(cc.delayTime(1.8),cc.spawn(cc.fadeTo(1.5,255),cc.scaleTo(3,1.5,2.3)),cc.spawn(cc.fadeTo(2.0,0),cc.scaleTo(3,0,0)),cc.callFunc(function() {stirr.removeFromParent(true);}));
				stirr.runAction(seq);
				electrode.stir4();
			}else if(gg.flow.flow.action==ACTION_DO8){
				var beaker=this.getChildByTag(TAG_BEAKER3);
				beaker.setVisible(false);
				var scrub1=new cc.Sprite("#equitment/scrub.png");
				scrub1.setOpacity(200);
				scrub1.setPosition(cc.p(gg.c_width*1.45,gg.c_height*0.8));
				this.addChild(scrub1,5,TAG_SCRUB1);
				var moveto=cc.moveTo(0.8,cc.p(gg.c_width*1.45,gg.c_height*1.1));
				var moveto1=cc.moveTo(0.8,cc.p(gg.c_width*1.45,gg.c_height*0.8));
				scrub1.runAction(cc.sequence(moveto,moveto1,moveto,moveto1, cc.callFunc(function() {scrub1.removeFromParent(true);})));
				electrode.whipping();
			}else if(gg.flow.flow.action==ACTION_DO9){
				var beaker=this.getChildByTag(TAG_BEAKER4);
				beaker.setVisible(false);
				electrode.whipping();
			}else if(gg.flow.flow.action==ACTION_DO10){
				var beaker=this.getChildByTag(TAG_BEAKER5);
				beaker.setVisible(false);
				var scrub2=new cc.Sprite("#equitment/scrub.png");
				scrub2.setOpacity(200);
				scrub2.setPosition(cc.p(gg.c_width*1.45,gg.c_height*0.8));
				this.addChild(scrub2,5,TAG_SCRUB2);
				var moveto=cc.moveTo(0.8,cc.p(gg.c_width*1.45,gg.c_height*1.1));
				var moveto1=cc.moveTo(0.8,cc.p(gg.c_width*1.45,gg.c_height*0.8));
				scrub2.runAction(cc.sequence(moveto,moveto1,moveto,moveto1, cc.callFunc(function() {scrub2.removeFromParent(true);})));
				electrode.whipping();
			}else if(gg.flow.flow.action==ACTION_DO12){
				var beaker=this.getChildByTag(TAG_BEAKER7);
				beaker.setVisible(false);
				var scrub3=new cc.Sprite("#equitment/scrub.png");
				scrub3.setOpacity(200);
				scrub3.setPosition(cc.p(gg.c_width*1.45,gg.c_height*0.8));
				this.addChild(scrub3,5,TAG_SCRUB);
				var moveto=cc.moveTo(0.8,cc.p(gg.c_width*1.45,gg.c_height*1.1));
				var moveto1=cc.moveTo(0.8,cc.p(gg.c_width*1.45,gg.c_height*0.8));
				scrub3.runAction(cc.sequence(moveto,moveto1,moveto,moveto1, cc.callFunc(function() {scrub3.removeFromParent(true);})));
				electrode.whipping();
			}else if(gg.flow.flow.action==ACTION_DO13){
				var stirr = new cc.Sprite("#equitment/stir.png")
				stirr.setPosition(cc.p(gg.c_width*1.24,gg.c_height*0.470));
				stirr.setOpacity(0);
				this.addChild(stirr, 5, TAG_STIRR);
				var seq = cc.sequence(cc.delayTime(1.8),cc.spawn(cc.fadeTo(1.5,255),cc.scaleTo(3,1.5,2.3)),cc.spawn(cc.fadeTo(2.0,0),cc.scaleTo(3,0,0)),cc.callFunc(function() {stirr.removeFromParent(true);}));
				stirr.runAction(seq);
				electrode.stir5();
			}
			break;
		case TAG_KETTLE:
			var rotate = cc.rotateTo(1, -60);
			var rotate1=cc.rotateTo(1,0);
			var seq=cc.sequence(rotate,func,cc.delayTime(1.8),rotate1,cc.fadeOut(0.5),cc.callFunc(function(){
				p.removeFromParent(true);
			},this),func1);
			p.runAction(seq);

			break;
		case TAG_KETTLE1:
			var rotate = cc.rotateTo(1, -60);
			var rotate1=cc.rotateTo(1,0);
			var seq=cc.sequence(rotate,func,cc.delayTime(1.8),rotate1,cc.fadeOut(0.5),cc.callFunc(function(){
				p.removeFromParent(true);
			},this),func1);
			p.runAction(seq);
			break;
		case TAG_KETTLE2:
			var rotate = cc.rotateTo(1, -60);
			var rotate1=cc.rotateTo(1,0);
			var seq=cc.sequence(rotate,func,cc.delayTime(1.8),rotate1,cc.fadeOut(0.5),cc.callFunc(function(){
				p.removeFromParent(true);
			},this),func1);
			p.runAction(seq);
			break;
		case TAG_KETTLE3:
			var rotate = cc.rotateTo(1, -60);
			var rotate1=cc.rotateTo(1,0);
			var seq=cc.sequence(rotate,func,cc.delayTime(1.8),rotate1,cc.fadeOut(0.5),cc.callFunc(function(){
				p.removeFromParent(true);
			},this),func1);
			p.runAction(seq);
			break;
		case TAG_BEAKER:
			var beaker=this.getChildByTag(TAG_BEAKER);
			if(gg.flow.flow.action==ACTION_DO1){
				beaker.move1();
			}else{
				beaker.move();
			}
			break;
		
		case TAG_BEAKER1:
			var beaker=this.getChildByTag(TAG_BEAKER1);
			beaker.move();
			break;
		case TAG_BEAKER2:
			var beaker=this.getChildByTag(TAG_BEAKER2);
			beaker.move();
			break;
		case TAG_BEAKER3:
			var beaker=this.getChildByTag(TAG_BEAKER3);
			if(gg.flow.flow.action==ACTION_DO8){
				beaker.move1();
			}else{
				beaker.move();
			}
			break;
		case TAG_BEAKER4:
			var beaker=this.getChildByTag(TAG_BEAKER4);
			if(gg.flow.flow.action==ACTION_DO9){
				beaker.move1();
			}else{
				beaker.move();
			}
			break;
		case TAG_BEAKER5:
			var beaker=this.getChildByTag(TAG_BEAKER5);
			if(gg.flow.flow.action==ACTION_DO10){
				beaker.move1();
			}else{
				beaker.move();
			}
			break;
		case TAG_BEAKER6:
			var beaker=this.getChildByTag(TAG_BEAKER6);
			if(gg.flow.flow.action==ACTION_DO11){
				beaker.move1();
			}else{
				beaker.move();
			}
			break;
		case TAG_BEAKER7:
			var beaker=this.getChildByTag(TAG_BEAKER7);
			if(gg.flow.flow.action==ACTION_DO12){
				beaker.move1();
			}else{
				beaker.move();
			}
			break;
		case TAG_INSTRUMENT:
			this.scene.instrument.show();
			p.runAction(func1);
			break;
		default:
		break;
		}
		
	},
	actionDone:function(p){
		var func = cc.callFunc(this.actionDone, this);
		switch(p.getTag()){
		case TAG_KETTLE:
			var beaker=this.getChildByTag(TAG_BEAKER);
			beaker.upLine();
			break;
		case TAG_KETTLE1:
			var beaker=this.getChildByTag(TAG_BEAKER3);
			beaker.upLine();
			break;
		case TAG_KETTLE2:
			var beaker=this.getChildByTag(TAG_BEAKER5);
			beaker.upLine();
			break;
		case TAG_KETTLE3:
			var beaker=this.getChildByTag(TAG_BEAKER7);
			beaker.upLine();
			break;
		case TAG_CALIBRATION:
			var beaker=this.getChildByTag(TAG_BEAKER1);
			beaker.upLine();
			break;
			default:
				break;
		}
	},
	flowNext:function(){
		gg.flow.next();
	},
	onExit:function(){
		this._super();
		this.callNext.release();
	}
});